<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="评估分组" prop="surveyId">
        <el-select v-model="queryParams.surveyId" placeholder="请选择分组" filterable clearable style="width: 240px">
          <el-option
              v-for="item in surveyList"
              :key="item.id"
              :label="item.title"
              :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input
            v-model="queryParams.title"
            placeholder="请输入标题"
            clearable
            style="width: 240px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="详情" prop="des">
        <el-input
            v-model="queryParams.des"
            placeholder="请输入详情"
            clearable
            style="width: 240px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="必答题" prop="isRequired">
        <el-select v-model="queryParams.isRequired" placeholder="全部" clearable style="width: 240px">
          <el-option label="否" value="0"/>
          <el-option label="是" value="1"/>
        </el-select>
      </el-form-item>
      <el-form-item label="题目类型" prop="questionType">
        <el-select v-model="queryParams.questionType" placeholder="请选择题目类型" filterable clearable
                   style="width: 240px">
          <el-option
              v-for="dict in question_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="出现填空框" prop="showInput">
        <el-select v-model="queryParams.showInput" placeholder="全部" clearable style="width: 240px">
          <el-option label="否" value="0"/>
          <el-option label="是" value="1"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
        >新增
        </el-button>
      </el-col>
    </el-row>

    <!--       @selection-change="handleSelectionChange"
          <el-table-column type="selection" width="55" align="center"/>-->
    <el-table v-loading="loading" :data="questionList" border>
      <el-table-column label="序号" align="center" type="index" width="80"/>
      <el-table-column label="分组名称" align="center" prop="surveyTitle" :show-overflow-tooltip="true"/>
      <el-table-column label="标题" align="center" prop="title" :show-overflow-tooltip="true"/>
      <el-table-column label="排序" align="center" prop="sort" :show-overflow-tooltip="true"/>
      <el-table-column label="详情" align="center" prop="des" :show-overflow-tooltip="true"/>
      <el-table-column label="必答题" align="center" prop="isRequired">
        <template #default="scope">
          <div v-if="scope.row.isRequired=='1'">是</div>
          <div v-if="scope.row.isRequired=='0'">否</div>
        </template>
      </el-table-column>
      <el-table-column label="题目类型" align="center" prop="questionType" :show-overflow-tooltip="true"/>
      <el-table-column label="问题内容" align="center" prop="content" :show-overflow-tooltip="true"/>
      <el-table-column label="出现填空框" align="center" prop="showInput" :show-overflow-tooltip="true">
        <template #default="scope">
          <div v-if="scope.row.showInput=='1'">是</div>
          <div v-if="scope.row.showInput=='0'">否</div>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
              type="text"
              icon="Edit"
              @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
              type="text"
              icon="Delete"
              @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改问卷题目对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body :close-on-click-modal="false">
      <el-form ref="questionRef" :model="form" :rules="rules" label-width="90px">
        <el-row :gutter="20" class="mb8">
          <el-col :span="12">
            <el-form-item label="评估分组" prop="surveyId">
              <el-select v-model="form.surveyId" placeholder="请选择分组" filterable clearable>
                <el-option
                    v-for="item in surveyList"
                    :key="item.id"
                    :label="item.title"
                    :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.questionType != '其他'">
            <el-form-item label="标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入标题"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="题目类型" prop="questionType">
              <el-select v-model="form.questionType" placeholder="请选择题目类型" filterable clearable>
                <el-option
                    v-for="dict in question_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="详情" prop="des">
              <el-input v-model="form.des" type="textarea" placeholder="请输入详情"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.questionType == '单选' || form.questionType == '多选'">
            <el-form-item label="排列方式" prop="placement">
              <el-select v-model="form.placement" placeholder="请选择排列方式" filterable clearable>
                <el-option
                    v-for="dict in arrangement"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="必答题" prop="isRequired">
              <el-radio-group v-model="form.isRequired">
                <el-radio label="0">否</el-radio>
                <el-radio label="1">是</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出现填空框" prop="showInput">
              <el-radio-group v-model="form.showInput">
                <el-radio label="0">否</el-radio>
                <el-radio label="1">是</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序" prop="sort">
              <el-input-number v-model="form.sort" :min="0"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="问题内容">
          <!--          <editor v-model="form.content" :min-height="192"/>-->
          <el-input v-model="form.content" type="textarea" placeholder="请输入内容"/>
          <div style="color: red;">注：单选或多选题，请填写选项内容，多个选项请用英文逗号隔开</div>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Question">
import {
  listQuestion,
  getQuestion,
  delQuestion,
  addQuestion,
  updateQuestion
} from "@/views/pg/questionnaire/api/question";
import {listAllSurvey} from "@/views/pg/questionnaire/api/survey.js";

const {proxy} = getCurrentInstance();
const {question_type, arrangement} = proxy.useDict('question_type', 'arrangement');

const questionList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const surveyList = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    surveyId: null,
    title: null,
    sort: null,
    des: null,
    isRequired: null,
    questionType: null,
    content: null,
    showInput: null,
    remark: null,
  },
  rules: {
    surveyId: [
      {required: true, message: "问卷不能为空", trigger: "blur"}
    ],
    title: [
      {required: true, message: "标题不能为空", trigger: "blur"}
    ],
    questionType: [
      {required: true, message: "题目类型不能为空", trigger: "blur"}
    ],
    placement: [
      {required: true, message: "排列方式不能为空", trigger: "blur"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

onMounted(() => {
  console.log("pagePath:question")
})

getList();
getSurveyList();

/** 查询问卷列表 */
function getSurveyList() {
  listAllSurvey().then(res => {
    surveyList.value = res.data;
  })
}

/** 查询问卷题目列表 */
function getList() {
  loading.value = true;
  listQuestion(queryParams.value).then(response => {
    questionList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    surveyId: null,
    title: null,
    sort: 0,
    des: null,
    isRequired: '0',
    questionType: null,
    content: null,
    showInput: '0',
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    deleteStatus: "1"
  };
  proxy.resetForm("questionRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加问卷题目";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getQuestion(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改问卷题目";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["questionRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateQuestion(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addQuestion(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除？').then(function () {
    return delQuestion(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('questionnaire/question/export', {
    ...queryParams.value
  }, `question_${new Date().getTime()}.xlsx`)
}

</script>
